//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.users
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.3 用户
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoUsers extends DoAPI
	{
		public function DoUsers(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_USERS_SHOW:String = 'http://api.t.163.com/users/show.json';
		public static const CMD_USERS_SHOW:String = 'cmdUsersShow';
		
		private static const URL_USERS_SUGGESTIONS:String = 'http://api.t.163.com/users/suggestions.json';
		public static const CMD_USERS_SUGGESTIONS:String = 'cmdUsersSuggestions';
		
		private static const URL_USERS_SUGGESTIONS_I:String = 'http://api.t.163.com/users/suggestions_i_followers.json';
		public static const CMD_USERS_SUGGESTIONS_I:String = 'cmdUsersSuggestionsI';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 获取指定用户的信息
		 * <br/>
		 * user_id、name、screen_name三个参数如果都不传，则返回当前用户信息
		 * 
		 * @param name
		 * @param user_id
		 * @param screen_name
		 */		
		public function getUserInfo(name:String, user_id:String, screen_name:String):void
		{
			var paras:Array = [];
			if (name != null && name != '')
				paras.push(new HttpParameter('name', name));
			
			if (user_id != null && user_id != '')
				paras.push(new HttpParameter('user_id', user_id));
					
			if (screen_name != null && screen_name != '')
				paras.push(new HttpParameter('screen_name', screen_name));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_USERS_SHOW, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
		  send(url, resultHandler, errorHandler);
			
		  function resultHandler(params:String):void
		  {
			  resultResponse(CMD_USERS_SHOW, params);
		  }
		  
		  function errorHandler(params:String):void
		  {
			  errorResponse(CMD_USERS_SHOW, params);
		  }
		}
		
		/**
		 * 返回用户可能感兴趣的用户，随机返回指定数目的用户
		 * 
		 * @param count 可选参数，指定随机返回的用户数目。默认为30个，最多返回30个
		 * @param trim_user 可选参数，值为true时返回最基本的用户数据，使用此参数可以减少返回数据量，默认为false
		 */		
		public function getSuggestions(count:String, trim_user:Boolean):void
		{
			var paras:Array = [];
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_USERS_SUGGESTIONS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_USERS_SUGGESTIONS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_USERS_SUGGESTIONS, params);
			}
		}
		
		/**
		 * 获取推荐的I达人的用户列表
		 * 
		 * @param cursor 可选参数，分页参数
		 * @param trim_user 可选参数，值为true时返回最基本的用户数据，使用此参数可以减少返回数据量，默认为false
		 */		
		public function getSuggestionsI(cursor:String, trim_user:Boolean):void
		{
			var paras:Array = [];
			if (cursor != null && cursor != '')
				paras.push(new HttpParameter('cursor', cursor));
			
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_USERS_SUGGESTIONS_I, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_USERS_SUGGESTIONS_I, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_USERS_SUGGESTIONS_I, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
		
	}
}